//
//  ZTXSelectedTeamViewController.m
//  NtoccShipper
//
//  Created by 张天兴 on 2024/11/11.
//  Copyright © 2024 zhuchongjin. All rights reserved.
//

#import "ZTXSelectedTeamViewController.h"
#import "CarTeamCell.h"
#import "CarTeamModel.h"

@interface ZTXSelectedTeamViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;
@property (weak, nonatomic) IBOutlet UITextField *searchTextField;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIImageView *imgVSearch;

@end

@implementation ZTXSelectedTeamViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:@"司机列表"];
    [self setBackLeftButton:@""];
    [self.tableView registerNib:[UINib nibWithNibName:@"CarTeamCell" bundle:nil] forCellReuseIdentifier:@"CarTeamCell"];
    [self.view addSubview:self.tableView];
    [self setRightImage:CJLString(@"去除代收人") image:@""];
    self.massageButton.titleLabel.font = [UIFont systemFontOfSize:14];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
    self.modelArray = [@[] mutableCopy];
    [self loadData];
    self.imgVSearch.userInteractionEnabled = YES;
    [self.imgVSearch setTapActionWithBlock:^{
//        [ws downRefresh];
    }];
}
- (void)RightTextClick:(UIButton *)sender{
    if (self.blockSeletModel) {
        CarTeamModel *model = [[CarTeamModel alloc] init];
           self.blockSeletModel(model);
       }
       [self pop];
    
}


//- (IBAction)btnSureAction:(id)sender {
    
//    NSMutableArray *arraySelelct = [[NSMutableArray alloc]init];
//    [self.modelArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        DriverListModel *model = obj;
//        if (model.selected) {
//            [arraySelelct addObject:model];
//        }
//    }];
//
//    if (arraySelelct.count > 0) {
//        if (self.blockSeletArr) {
//            self.blockSeletArr(arraySelelct);
//        }
//        [self pop];
//    }else{
//        [CJAlert showStringDismissWithDelay:@"请选择司机"];
//    }
//}

/// <#Description#>
- (void)loadData {

    NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/orderDetail/getBandCaptainByOrder",CJBaseUrl);
    NSDictionary *dicPar = @{@"codes":self.strCode,
    };
    
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
                
        NSArray *list = responseObject;
        NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);
//        if (list.count > 0) {
            if ([ws.refreshType isEqualToString:@"down"]) {
                [ws.modelArray removeAllObjects];
            }
            
            [list enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
               NSError *error;
                CarTeamModel *model = [[CarTeamModel alloc]initWithDictionary:obj error:&error];
//                if([model.teamCode isEqualToString:self.teamCode]){
//                    model.isSelected = YES;
//                }else{
//                    model.isSelected = NO;
//                }
                [ws.modelArray addObject:model];
            }];
            
//            [ws.tableView.mj_header endRefreshing];
//            [ws.tableView.mj_footer endRefreshing];
            
            if (ws.modelArray.count == total.intValue) {
//                [ws.tableView.mj_footer endRefreshingWithNoMoreData];
            }
           
//        }else{
//
//            [ws.tableView.mj_header endRefreshing];
//            [ws.tableView.mj_footer endRefreshingWithNoMoreData];
//        }
        
       [ws.tableView reloadData];
        
        
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];

}

- (void)downRefresh{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"CarTeamCell";
    CarTeamCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CarTeamModel *model = self.modelArray[indexPath.row];
    if([self.strCode isEqualToString:model.partyCode]){
        model.isSelected = YES;
    }else{
        model.isSelected = NO;
    }
    [cell setModel:model];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    CarTeamModel *model = self.modelArray[indexPath.row];
    if (model.isSelected) {
        model.isSelected = NO;
    }else{
        model.isSelected = YES;
    }
//    [self.tableView reloadData];
    if (self.blockSeletModel) {
           self.blockSeletModel(model);
       }
       [self pop];
    
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, kTopHeight +  CJTableToTopHeight + 5, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight  - CJTableToTopHeight - 5),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(NO)
        ;
//        WS(ws);
//        _tableView.blockMjHeader = ^(BOOL isheader) {
//
//            if (isheader) {
//                [ws downRefresh];
//            } else {
//                [ws upRefresh];
//            }
//        };
    }
    return _tableView;
}

@end
